package br.com.edumobi.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Arrays;

import br.com.edumobi.R;
import br.com.edumobi.dados.modelos.Aluno;
import br.com.edumobi.dados.modelos.enuns.Sexo;
import br.com.edumobi.utils.Dates;
import br.com.edumobi.utils.Mask;

public class DadosAlunoFragment extends GenericoFragment {

    private TextView textViewNomeDadosAlunoFragment;
    private TextView textViewEmailDadosAlunoFragment;
    private TextView textViewCpfDadosAlunoFragment;
    private TextView textViewSexoDadosAlunoFragment;
    private TextView textViewDataNascimentoDadosAlunoFragment;
    private EditText editTextNomeDadosAlunoFragment;
    private EditText editTextEmailDadosAlunoFragment;
    private EditText editTextCpfDadosAlunoFragment;
    private EditText editTextDataDeNascimentoDadosAlunoFragment;
    private Spinner spinnerSexoDadosAlunoFragment;
    private Button buttonSalvarDadosAlunoFragment;
    private ProgressBar progressBarDadosAlunoFragment;

    private AcoesDadosAlunoFragment activity;
    private DadosAlunoTask task;

    private Aluno aluno;

    View.OnClickListener clickButtonSalvarDadosAlunoFragment = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String nome = editTextNomeDadosAlunoFragment.getText().toString();
            String email = editTextEmailDadosAlunoFragment.getText().toString();
            String cpf = editTextCpfDadosAlunoFragment.getText().toString();

            if (nome.isEmpty()) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_nome);
            } else if (email.isEmpty()) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_email);
            } else if (cpf.isEmpty()) {
                activity.falarEExibirTexto(R.string.obrigatoriedade_cpf);
            } else {
                if (activity.conectado()) {
                    aluno.nome = nome;
                    aluno.email = email;
                    aluno.cpf = cpf;
                    aluno.sexo = Sexo.getEnum((String) spinnerSexoDadosAlunoFragment.getSelectedItem());
                    aluno.dataDeNascimento = Dates.stringParaDate(editTextDataDeNascimentoDadosAlunoFragment.getText().toString());

                    if (task == null || task.getStatus().equals(AsyncTask.Status.FINISHED)) {
                        task = new DadosAlunoTask();
                        task.execute();
                    }
                } else {
                    activity.falarEExibirTexto(R.string.excecao_sem_conexao_internet);
                }
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity = (AcoesDadosAlunoFragment) getActivity();

        aluno = activity.obterAluno();

        editTextNomeDadosAlunoFragment.setText(aluno.nome);
        editTextEmailDadosAlunoFragment.setText(aluno.email);
        editTextCpfDadosAlunoFragment.setText(aluno.cpf);
        editTextDataDeNascimentoDadosAlunoFragment.setText(Dates.dateParaString(aluno.dataDeNascimento));
        spinnerSexoDadosAlunoFragment.setSelection(aluno.sexo != null ? Arrays.asList(getActivity().getResources().getStringArray(R.array.generos)).indexOf(aluno.sexo.sexo) : 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dados_aluno, container, false);

        textViewNomeDadosAlunoFragment = (TextView) view.findViewById(R.id.textViewNomeDadosAlunoFragment);
        textViewEmailDadosAlunoFragment = (TextView) view.findViewById(R.id.textViewEmailDadosAlunoFragment);
        textViewCpfDadosAlunoFragment = (TextView) view.findViewById(R.id.textViewCpfDadosAlunoFragment);
        textViewSexoDadosAlunoFragment = (TextView) view.findViewById(R.id.textViewSexoDadosAlunoFragment);
        textViewDataNascimentoDadosAlunoFragment = (TextView) view.findViewById(R.id.textViewDataNascimentoDadosAlunoFragment);
        editTextNomeDadosAlunoFragment = (EditText) view.findViewById(R.id.editTextNomeDadosAlunoFragment);
        editTextEmailDadosAlunoFragment = (EditText) view.findViewById(R.id.editTextEmailDadosAlunoFragment);
        editTextCpfDadosAlunoFragment = (EditText) view.findViewById(R.id.editTextCpfDadosAlunoFragment);
        editTextDataDeNascimentoDadosAlunoFragment = (EditText) view.findViewById(R.id.editTextDataDeNascimentoDadosAlunoFragment);
        spinnerSexoDadosAlunoFragment = (Spinner) view.findViewById(R.id.spinnerSexoDadosAlunoFragment);
        buttonSalvarDadosAlunoFragment = (Button) view.findViewById(R.id.buttonSalvarDadosAlunoFragment);
        progressBarDadosAlunoFragment = (ProgressBar) view.findViewById(R.id.progressBarDadosAlunoFragment);

        editTextCpfDadosAlunoFragment.addTextChangedListener(Mask.insert("###.###.###-##", editTextCpfDadosAlunoFragment));
        editTextDataDeNascimentoDadosAlunoFragment.addTextChangedListener(Mask.insert("##/##/####", editTextDataDeNascimentoDadosAlunoFragment));
        buttonSalvarDadosAlunoFragment.setOnClickListener(clickButtonSalvarDadosAlunoFragment);

        if (task != null && task.getStatus().equals(AsyncTask.Status.RUNNING)) {
            esconderComponentes();
            progressBarDadosAlunoFragment.setVisibility(View.VISIBLE);
        }

        return view;
    }

    @Override
    public void exibirComponentes() {
        textViewNomeDadosAlunoFragment.setVisibility(View.VISIBLE);
        textViewEmailDadosAlunoFragment.setVisibility(View.VISIBLE);
        textViewCpfDadosAlunoFragment.setVisibility(View.VISIBLE);
        textViewSexoDadosAlunoFragment.setVisibility(View.VISIBLE);
        textViewDataNascimentoDadosAlunoFragment.setVisibility(View.VISIBLE);
        editTextNomeDadosAlunoFragment.setVisibility(View.VISIBLE);
        editTextEmailDadosAlunoFragment.setVisibility(View.VISIBLE);
        editTextCpfDadosAlunoFragment.setVisibility(View.VISIBLE);
        editTextDataDeNascimentoDadosAlunoFragment.setVisibility(View.VISIBLE);
        spinnerSexoDadosAlunoFragment.setVisibility(View.VISIBLE);
        buttonSalvarDadosAlunoFragment.setVisibility(View.VISIBLE);
        progressBarDadosAlunoFragment.setVisibility(View.VISIBLE);
    }

    @Override
    public void esconderComponentes() {
        textViewNomeDadosAlunoFragment.setVisibility(View.GONE);
        textViewEmailDadosAlunoFragment.setVisibility(View.GONE);
        textViewCpfDadosAlunoFragment.setVisibility(View.GONE);
        textViewSexoDadosAlunoFragment.setVisibility(View.GONE);
        textViewDataNascimentoDadosAlunoFragment.setVisibility(View.GONE);
        editTextNomeDadosAlunoFragment.setVisibility(View.GONE);
        editTextEmailDadosAlunoFragment.setVisibility(View.GONE);
        editTextCpfDadosAlunoFragment.setVisibility(View.GONE);
        editTextDataDeNascimentoDadosAlunoFragment.setVisibility(View.GONE);
        spinnerSexoDadosAlunoFragment.setVisibility(View.GONE);
        buttonSalvarDadosAlunoFragment.setVisibility(View.GONE);
        progressBarDadosAlunoFragment.setVisibility(View.GONE);
    }

    private class DadosAlunoTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            esconderComponentes();
            progressBarDadosAlunoFragment.setVisibility(View.VISIBLE);
            activity.falarTexto(R.string.aguarde);
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            return activity.salvarDadosAluno(aluno);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            exibirComponentes();
            progressBarDadosAlunoFragment.setVisibility(View.GONE);

            if (aBoolean) {
                activity.falarEExibirTexto(R.string.dados_salvos_sucesso);
                activity.salvarPreferencesAluno(aluno);
            } else {
                activity.falarEExibirTexto(R.string.excecao_atualizacao_dados_aluno_malsucedida);
            }
        }
    }

    public interface AcoesDadosAlunoFragment extends AcoesGenericoFragment {
        Aluno obterAluno();
        Boolean salvarDadosAluno(Aluno aluno);
    }
}
